(function(window, jquery) {
	let ajax = function(url, param, type = 'GET') {
		const promise = new Promise(function(resolve, reject) {
			const clearObjVal = (obj) => {
				obj = obj || {}
				if (typeof obj == 'object') {
					Object.keys(obj).forEach(key => {
						if (obj[key] === '') {
							delete obj[key]
						}
					})
				}
				return JSON.parse(JSON.stringify(obj))
			}
			let headers = {},
				tc_token = getCookie('tc_token')
			if(tc_token){
				headers.token = tc_token
			}
			$.ajax({
				type: type,
				url: domain + url,
				data: clearObjVal(param),
				headers: headers,
				dataType: 'json',
				success(res) {
					if(res && res.code == 505){
						delCookie('tc_token')
						layer.msg(res.msg)
						setTimeout(function() {
							location.href = loginUrl
						}, 1500);
						return 
					}
					resolve(res)
				},
				error(res) {
					reject('响应错误')
					// reject(res.statusText)
				}
			})
		})
		return promise
	}
	window.post = function(url, param) {
		return ajax(url, param, 'post')
	}
	window.get = function(url, param) {
		return ajax(url, param, 'get')
	}
	window.getUserInfo = function(callback){
		if(!getCookie('tc_token')){ return }
		post('/imgbed/user/info').then(res=>{
			if(res && res.code == 0){
				res = res.data
				$('#contextMenuExample2 .t').html(`${res.phoneNum}&nbsp;&nbsp;${res.usedSpace}/${res.totalSpace}`)
				callback && callback(res)
			}else{
				layer.msg(res.msg)
			}
		})
	}
	window.getpage = function(url, callback) {
		let load = function(val) {
			setTimeout(() => {
				$('.nomore-load').hide()
				$(`#load${val}`).show()
				$('#loadflag').val(val)
			}, 0)
		}
		let currentpage = $('#currentpage').val(),
			param = {},
			type = $('#type').val()
		param.page = currentpage
		if (type !== undefined) {
			param.type = type
		}
		window.get(url, param).then(res => {
			if (res && res.code == 200) {
				let a = callback && callback(res.data.data)
				$('#list-content').append(a)
				if (res.data.per_page > res.data.data.length) {
					load(0)
				} else {
					currentpage++
					$('#currentpage').val(currentpage)
					load(1)
				}
			} else {
				load(1)
			}
		}).catch(err => {
			load(1)
		})
	}
	window.scroll_page = function(url, callback) {
		$(window).scroll(function() {
			var scrollTop = $(this).scrollTop();
			var scrollHeight = $(document).height();
			var windowHeight = $(this).height();
			if (scrollTop + windowHeight > (scrollHeight - 100)) {
				let loadflag = $('#loadflag').val()
				if (loadflag == 1) {
					$('#loadflag').val(0)
					window.getpage(url, callback)
				}
			}
		})
	}
	// 浮点数相除
	window.floatAccDiv = function(arg1, arg2) {
		let t1 = 0,
			t2 = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			t1 = s1.split(".")[1].length;
		} catch (e) {}
		try {
			t2 = s2.split(".")[1].length;
		} catch (e) {}
		return (Number(s1.replace(".", "")) / Number(s2.replace(".", ""))) * Math.pow(10, t2 - t1);
	}
	// 浮点数相乘
	window.floatAccMul = function(arg1, arg2) {
		let m = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			m += s1.split(".")[1].length;
		} catch (e) {}
		try {
			m += s2.split(".")[1].length;
		} catch (e) {}
		return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
	}
	// 浮点数加法
	window.floatAccAdd = function(arg1, arg2) {
		let r1 = 0,
			r2 = 0,
			m, c, s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			r1 = s1.split(".")[1].length;
		} catch (e) {}
		try {
			r2 = s2.split(".")[1].length;
		} catch (e) {}
		c = Math.abs(r1 - r2);
		m = Math.pow(10, Math.max(r1, r2));
		arg1 = Number(s1.replace(".", ""));
		arg2 = Number(s2.replace(".", ""));
		if (c > 0) {
			let cm = Math.pow(10, c);
			if (r1 > r2) {
				arg2 = Number(s2.replace(".", "")) * cm;
			} else {
				arg1 = Number(s1.replace(".", "")) * cm;
			}
		}
		return (arg1 + arg2) / m;
	}
	// 浮点数减法
	window.floatAccSub = function(arg1, arg2) {
		let r1 = 0,
			r2 = 0,
			m, n;
		try {
			r1 = arg1.toString().split(".")[1].length;
		} catch (e) {}
		try {
			r2 = arg2.toString().split(".")[1].length;
		} catch (e) {}
		m = Math.pow(10, Math.max(r1, r2));
		//动态控制精度长度
		n = (r1 >= r2) ? r1 : r2;
		return Number(((arg1 * m - arg2 * m) / m).toFixed(n));
	}
	//保留两位小数
	window.toDecimal2 = function(x) {
		var f = parseFloat(x);
		if (isNaN(f)) {
			return false;
		}
		var f = Math.floor(x * 100) / 100;
		var s = f.toString();
		var rs = s.indexOf('.');
		if (rs < 0) {
			rs = s.length;
			s += '.';
		}
		while (s.length <= rs + 2) {
			s += '0';
		}
		return s;
	}
	//设置cookie
	window.setCookie = function(key, value, time) {
		//30天 24小时 60分钟 60秒 毫秒为单位
		//后台shiro没有设置rememberMe 所以重开浏览器会导致cookie失效
		//传入key value 和以毫秒为单位的时间
		var exp = new Date();
		exp.setTime(exp.getTime() + time);
		document.cookie = key + "=" + value + ";expires=" + exp.toGMTString();
	}
	window.getCookie = function(cookieName) {
		//使用字符串匹配的方式
		if (document.cookie.length > 0) {
			var start = document.cookie.indexOf(cookieName + "=")
			if (start != -1) {
				start = start + cookieName.length + 1
				var end = document.cookie.indexOf(";", start)
				if (end == -1) end = document.cookie.length
				return unescape(document.cookie.substring(start, end))
			}
		}
		return null
	}
	//删除cookie
	window.delCookie = function(name) {
		//将cookie有效期设置为<0
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = getCookie(name);
		if (cval != null)
			document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
	}
	window.queryURLParams = function(name){
		 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		 var r = window.location.search.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
		 if(r!=null)return  unescape(r[2]); return null;
	}

	
})(window, $)
//
//post(url, {a: 1,b:2}).then(res=>{
//	if(res && res.code == 200){
//		//success code 200
//	}else{
//		//error code !=200
//	}
//})
//
//get(url, {a: 1,b:2}).then(res=>{
//	if(res && res.code == 200){
//		//success code 200
//	}else{
//		//error code !=200
//	}
//})
//
